package com.james.customcamera.presenter;

import com.james.customcamera.base.BaseModel;
import com.james.customcamera.base.BasePresenter;
import com.james.customcamera.bean.LoginBean;
import com.james.customcamera.contract.LoginContract;
import com.james.customcamera.model.LoginModel;
import com.james.customcamera.mvp.IModel;
import com.james.customcamera.ui.LoginActivity;
import com.james.customcamera.utils.SPUtils;

import java.util.HashMap;

/**
 * Created by James on 2018/1/31 0031.
 */

public class LoginPresenter extends BasePresenter<LoginActivity> implements LoginContract.LoginPresenter {

    @Override
    public void login(String userName, String password) {
        if (!getIView().checkNull()) {
            getIView().showLoading();
            ((LoginModel) getiModelMap().get("login")).login(userName, password
                    , new LoginModel.DataListener<LoginBean>() {

                        @Override
                        public void successInfo(LoginBean result) {
                            getIView().stopLoading();
                            getIView().LoginSuccess(result.getToken());
                        }

                        @Override
                        public void failInfo(String result) {
                            getIView().stopLoading();
                            getIView().LoginFail(result);
                        }
                    });
        }
    }

    @Override
    public HashMap<String, IModel> getiModelMap() {
        return loadModelMap(new LoginModel());
    }

    @Override
    public HashMap<String, IModel> loadModelMap(IModel... models) {
        HashMap<String, IModel> map = new HashMap<>();
        map.put("login", models[0]);
        return map;
    }
}
